$(function () {
    BASE.USERWITHDRAW.initMoney();
    BASE.USERWITHDRAW.getInitData();
});
var BASE = BASE || {};
BASE.USERWITHDRAW = (function () {
    /*查询手续费，和起提金额*/
    function getInitData() {
        var sendData = {
            codes:'withdrawMinMoney,withdrawCharge'
        };
        BASE.apiGet('/device/sysOptions/list',sendData,function (res) {
            res.forEach(function (v) {
                if(v.code == 'withdrawMinMoney'){
                    $('#withdraw_money').attr('placeholder',"输入金额（最低"+v.value+"元）").data('minmoney',v.value);
                    return;
                }
                $('#'+v.code).text(v.value*100);
            })
        })
    }
    function initMoney() {
        var href = location.href;
        var money = 0;
        if(href.indexOf('money=') >-1){
            money = href.split('?money=').pop();
        }
        $('#money').text(money);
        return money;
    }
    /*获取所有的绑定数据*/
    function getData(obj){
        var $this = $(obj);
        var type = $this.data('type');
        if($this.data('flag')){
            $this.next().slideToggle();
            return;
        }
        BASE.apiGet('/device/midno/list',{type:type},function (res) {
            $this.data('flag',true);
            $this.next().html(template('list_tpl'+type,{list:res}));
            $this.next().slideDown();
        })
    }
    /*添加卡之前*/
    function addPrev(obj){
        var type = $(obj).data('type');
        $('body').data('dom',$(obj).prevAll('.checked-account'));
        $('body').data('type',type);
        var $bindBox = $('#bindAccout');
        var arr = ['银行卡','支付宝账号','微信账号'];
        $bindBox.find('.title').text(arr[type-1]);
        if(type ==1){
            $bindBox.find('.card-bank').show();
        }else {
            $bindBox.find('.card-bank').hide();
        }
        $bindBox.show();
    }
   /* 确认绑定*/
    function sureBind(obj) {
        var sendData = {
          /*  name:$('#name').val(),*/
            idNo:$('#idNo').val(),
            bank:$('#bank').val(),
            type:$('body').data('type'),
        };
        if(!sendData.idNo){
            BASE.clearpop('请输入账号');
            return;
        }
        if(sendData==1 && !sendData.bank){
            BASE.clearpop('开户行必填');
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/midno/add',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.code !== 200){
                BASE.clearpop(res.message||'添加失败');
                return;
            }
            $('#idNo').val('');
            $('#bank').val('');
            $('body').data('dom').data('flag',false).trigger('click');
            $('#bindAccout').hide();
        })
    }
    /*全部提现*/
    function withdrawTotal() {
        var href = location.href;
        var money =0;
        if(href.indexOf('money=') >-1){
            money = href.split('?money=').pop();
        }
        $('#withdraw_money').val(money);
    }
   /*提现获取验证码*/
    function withdrawPrev(obj) {
        var data = _getWithDrawData();
        if(!data){
            return;
        }
        var userInfo = BASE.getStore('userInfo');
        if(!userInfo){
            BASE.clearpop('登录异常请重新登录');
            return;
        }
        var tel = userInfo.mobile;
        if(!tel){
            BASE.clearpop('请先在个人中心绑定手机号');
            return;
        }
        var sendData = {
            mobile:userInfo.mobile,
            vcodeStyle:4,
        };
        $(obj).attr('disabled',true);
        BASE.apiPost('/device/member/sendMobileCode', sendData, function () {
            BASE.clearpop('短信已成功发送到' + tel, 2000);
            $('#vcode').val('');
            BASE.mortime($(obj));
            $('#getCode').show();
        });
    }
    /*提现*/
    function withdraw(obj) {
        var sendData = _getWithDrawData();
        if(!sendData){
            return;
        }
        BASE.postHttp('/device/withdraw/add',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.ecode == 'REQUIRED_ARGUMENT_EXCEPTION'){
                BASE.clearpop('请先在个人中心完善资料');
                return;
            }
            if(res.ecode =='ILLEGAL_ARGUMENT_EXCEPTION'){
                BASE.clearpop('绑定的手机号有误');
                return;
            }
            if(res.code !== 200){
                BASE.clearpop(res.message||'提现失败');
                return;
            }
            $('#vcode').val('');
            $('#withdraw_money').val('');
            BASE.clearpop('申请提现成功');
            location.href = '/userCenter.html';
            $('#getCode').hide();
        });
    }
   /* 获取提现的数据*/
    function _getWithDrawData() {
        var type = $('body').data('style');
        var arr = ["bank","alipay","weixin"];
        var min =  $('#withdraw_money').data('minmoney');
        var sendData = {
            vcode:$('#vcode').val(),
            money:$('#withdraw_money').val(),
           /* style:arr[type-1],*/
            idNoId:$('body').data('idnoid')
        };
        var canWithdraw = initMoney();
        if(!sendData.money){
            BASE.clearpop('输入提现金额');
            return false;
        }
        if(sendData.money <min){
            BASE.clearpop('最低'+min+'元起提');
            return false;
        }
        if(!sendData.idNoId){
            BASE.clearpop('请选择提款账户');
            return false;
        }
        if(sendData.money - canWithdraw >0){
            BASE.clearpop('可提现金额为'+canWithdraw+'元');
            return false;
        }
        return sendData;
    }
    $('body').on('click','.withdraw-lists-item',function () {
        var $div = $(this).parents('ul').prev();
        $('body').data('idnoid',$(this).data('id'));
        $('body').data('style',$($div).data('type'));
        $(this).addClass('checked').siblings().removeClass('checked');
        $(this).parents('ul').slideUp();
        _initCheckedCard();
        $div.html('').append($(this).find('img').clone()).append($(this).find('span:eq(1)').clone());
    });
    function _initCheckedCard() {
        $('body .checked-account').each(function () {
            $(this).text('请选择账户');
        })
        $('body .withdraw-lists-item.checked').each(function () {
            $(this).removeClass('checked');
        })
    }
    /*删除提现账户*/
    function deleteWithAccout(obj) {
        var id =$(obj).data('id');
        BASE.apiPost('/device/midno/delete',{id:id},function () {
            $(obj).parents('.withdraw-lists').prev('.checked-account').data('flag',false).text('请选择账户').trigger('click');
        })
    }
    return{
        getInitData:getInitData,
        withdrawTotal:withdrawTotal,
        initMoney:initMoney,
        addPrev:addPrev,
        sureBind:sureBind,
        getData:getData,
        withdrawPrev:withdrawPrev,
        withdraw:withdraw,
        deleteWithAccout:deleteWithAccout,
    }
})();